# This script genates top "pivot" tokens for first two dimensions of each concept
# the readfunc() function both writes this output to a csv and prints a LaTeX table

source("pundits_functions.R")

readfunc <- function(path = "../output/parrots/",
                     file){
  require(tidyverse)
  concept <- gsub("_parrot", "", strsplit(file, "\\.")[[1]][1])
    
  load(paste0(path, file))    

  keys <- get_keywords_custom(scores = thel, 
                            n_dimensions = 2, 
                            n_words = 10,
                            topic = "china", capture_output = "TRUE")
       
  df <- bind_cols(keys[[1]],keys[[2]])
  names(df) <- c("Pivots_n1", "Pivots_p1", "Pivots_n2", "Pivots_p2")
  write.csv(df, file = paste0("../output/pivot_keywords/keywords_", concept, "_dim1dim2.csv"))
    
  df[,c(1,2)] %>%
    xtable::xtable() %>%
    print(include.rownames = FALSE)
  
  df[,c(3,4)] %>%
    xtable::xtable() %>%
    print(include.rownames = FALSE)
  
}

files <- list.files(path = "../output/parrots/")

for(f in files){

    readfunc(file = f)
    
}